home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / texinfo / info / xmalloc.c < prev   
C/C++ Source or Header  |  1994-01-28  |  2KB  |  79 lines

  1. /* xmalloc.c -- safe versions of malloc and realloc */
  2.  
  3. /* This file is part of GNU Info, a program for reading online documentation
  4.    stored in Info format.
  5.  
  6.    This file has appeared in prior works by the Free Software Foundation;
  7.    thus it carries copyright dates from 1988 through 1993.
  8.  
  9.    Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993 Free Software
  10.    Foundation, Inc.
  11.  
  12.    This program is free software; you can redistribute it and/or modify
  13.    it under the terms of the GNU General Public License as published by
  14.    the Free Software Foundation; either version 2, or (at your option)
  15.    any later version.
  16.  
  17.    This program is distributed in the hope that it will be useful,
  18.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.    GNU General Public License for more details.
  21.  
  22.    You should have received a copy of the GNU General Public License
  23.    along with this program; if not, write to the Free Software
  24.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25.  
  26.    Written by Brian Fox (bfox@ai.mit.edu). */
  27.  
  28. #if !defined (ALREADY_HAVE_XMALLOC)
  29. #include <stdio.h>
  30.  
  31. static void memory_error_and_abort ();
  32.  
  33. /* **************************************************************** */
  34. /*                                    */
  35. /*           Memory Allocation and Deallocation.            */
  36. /*                                    */
  37. /* **************************************************************** */
  38.  
  39. /* Return a pointer to free()able block of memory large enough
  40.    to hold BYTES number of bytes.  If the memory cannot be allocated,
  41.    print an error message and abort. */
  42. void *
  43. xmalloc (bytes)
  44.      int bytes;
  45. {
  46.   void *temp = (void *)malloc (bytes);
  47.  
  48.   if (!temp)
  49.     memory_error_and_abort ("xmalloc");
  50.   return (temp);
  51. }
  52.  
  53. void *
  54. xrealloc (pointer, bytes)
  55.      void *pointer;
  56.      int bytes;
  57. {
  58.   void *temp;
  59.  
  60.   if (!pointer)
  61.     temp = (void *)malloc (bytes);
  62.   else
  63.     temp = (void *)realloc (pointer, bytes);
  64.  
  65.   if (!temp)
  66.     memory_error_and_abort ("xrealloc");
  67.  
  68.   return (temp);
  69. }
  70.  
  71. static void
  72. memory_error_and_abort (fname)
  73.      char *fname;
  74. {
  75.   fprintf (stderr, "%s: Out of virtual memory!\n", fname);
  76.   abort ();
  77. }
  78. #endif /* !ALREADY_HAVE_XMALLOC */
  79.